const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cl_x:"0",
    gz_sq:"",
    hktupian:[],
    img_arr: [],
    page:0,
    zd_h:"",
    zd_lh:"",
    sh_name:"",
    sh_h:"",
    yy_dz:"",
    lx_r:"",
    lx_dh:"",
    business_id:'',
    equipment_id:''
  },
  zd_h:function(e){
    this.data.zd_h = e.detail.value
  },
  zd_lh:function(e){
    this.data.zd_lh = e.detail.value
  },
  sh_name:function(e){
    this.data.sh_name = e.detail.value
  },
  sh_h:function(e){
    this.data.sh_h = e.detail.value
  },
  yy_dz:function(e){
    this.data.yy_dz = e.detail.value
  },
  lx_r:function(e){
    this.data.lx_r = e.detail.value
  },
  lx_dh:function(e){
    this.data.lx_dh = e.detail.value
  },
  tijiao:function(e){
    var that = this;
    if(this.data.zd_h==''){
      wx.showToast({
        title: '请请输入终端号',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.sh_name==''){
      wx.showToast({
        title: '请输入商户名称',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.sh_h==''){
      wx.showToast({
        title: '请输入商户号',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.yy_dz==''){
      wx.showToast({
        title: '请输入营业地址',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.lx_r==''){
      wx.showToast({
        title: '请输入联系人',
        icon: 'none',
        duration: 2000
      })
    }else if(this.data.lx_dh==''){
      wx.showToast({
        title: '请输入联系人电话',
        icon: 'none',
        duration: 2000
      })
    }else if(app.globalData.file_12==''||app.globalData.file_12==null||app.globalData.file_12==undefined){
      wx.showToast({
        title: '请上传图片',
        icon: 'none',
        duration: 2000
      })
    }
    else{
      that.formDataSend()
    }
  },
  terminalSearch(){
    var that = this
    wx.request({
      url: app.globalData.url_l + 'applets/search?terminalnumber=' + this.data.zd_h,
      method:'GET',
      header:{
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        'token':wx.getStorageSync('token')
      },
      success(res){
        console.log(res.data.data)
        if(res.data.data){
          that.setData({
            business_id:res.data.data.business_id,
            equipment_id:res.data.data.equipment_id,
            sh_h:res.data.data.business.b_number,
            sh_name:res.data.data.business.business_name
          })
        }else{
          wx.showToast({
            title: '终端号不存在!',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  formDataSend(){
    var that = this
    wx.request({
      url: app.globalData.url_l + 'applets/search?terminalnumber=' + this.data.zd_h,
      method:'GET',
      header:{
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        'token':wx.getStorageSync('token')
      },
      success(res){
        if(res.data.data){
          wx.request({
            url: app.globalData.url_l + 'applets/apply?terminalnumber=' + that.data.zd_h + 
                 '&business_name=' + that.data.sh_name +
                 '&b_number=' + that.data.sh_h + 
                 '&businessAddress=' + that.data.yy_dz +
                 '&contacts=' + that.data.lx_r + 
                 '&contactsPhone=' + that.data.lx_dh +
                 '&type=' + that.data.cl_x + 
                 '&applyType=' + that.data.gz_sq +
                 '&picurl=' +  'https://' + that.data.img_arr[0].img +
                 '&business_id=' + res.data.data.business_id +
                 '&equipment_id='+ res.data.data.equipment_id,
            method:'POST',
            header:{
              'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
              'token':wx.getStorageSync('token')
            },
            success(success){
              console.log(success.data)
              if(success.data.code == 200){
                wx.showToast({
                  title: '提交成功!',
                  icon: 'none',
                  duration: 2000
                })
                wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })
        }else{
          wx.showToast({
            title: '终端号不存在!',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
   // 扫码
   getScancode: function () {
    var _this = this;
    var that = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      scanType: 'json',
      success: (res) => {
        console.log(res)
        app.globalData.bian = res.result
        wx.request({
          url: app.globalData.url_l + 'index/jieguo',
          method: 'post',
          data:{
            fangwei:res.result,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res)
            if(res.data.code==0){
              wx.showToast({
                title: '扫码失败！',
                icon: 'none',
                duration: 2000,                
            });
            }else if(res.data.code==1){
              app.globalData.cl_bh=res.data.data.bianhao
              app.globalData.cl_fwm=res.data.data.fw_hao
              app.globalData.cl_fzr=res.data.data.gl_name
               wx.showToast({
                title: '扫码成功！',
                icon: 'none',
                duration: 2000,
                success: function () {
                  that.setData({
                    shibie:res.data.data,
                    inputValue:res.data.data.bianhao
                  })
                }
            });
            }if(res.data.code==2){
              wx.showToast({
                title: '扫码失败！',
                icon: 'none',
                duration: 2000,
                
            });
            }                     
          }
        })   
      }
    })
  },
  upimg2: function () {
    var that = this;
    var hukoutu = [];
    var upload_picture_list = that.data.upload_picture_list
    app.globalData.qx_id=0
    if (this.data.img_arr.length < 9) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        sourceType:['album'],
        success: function (res) {
          console.log(res)
          var tempFiles = res.tempFiles
          var img_arr = res.tempFilePaths
          for (var i = 0; i < res.tempFilePaths.length; i++) {
            wx.showLoading({
              title: '上传中...',
              mask: true,
            })
            const uploadTask = wx.uploadFile({
              url: 'https://beijingyada.ascetic.top/api/base/appletsUpload',
              filePath: img_arr[i],
              name: 'file',
              formData:{
                'user':'test'
              },
              success: function (res) {
                //打印                
                console.log(JSON.parse(res.data).data)
                that.data.page++;
                that.data.hktupian.push(
                  {
                    id:that.data.page,
                    img:JSON.parse(res.data).data
                  });
                app.globalData.file_12 = that.data.hktupian;
                that.setData({
                  img_arr: app.globalData.file_12
                })
              }
            })
            uploadTask.onProgressUpdate((res) => {
              if(res.progress=='100'){
                setTimeout(function () {
                  wx.hideLoading()   //隐藏loading
                }, 1000)
              }
          })
          }          
        }
      })
      if(this.data.img_arr.length==1){
         this.setData({
           gao: 110 
         })
      } else if (this.data.img_arr.length == 2) {
        this.setData({
          gao: 110
        })
      }
      else if (this.data.img_arr.length == 3) {
        this.setData({
          gao: 220
        })
      }
      else if (this.data.img_arr.length == 4) {
        this.setData({
          gao: 220
        })
      }
      else if (this.data.img_arr.length == 5) {     
        this.setData({
          gao: 220
        })
      }
      else if (this.data.img_arr.length == 6) {
        this.setData({
          gao: 220
        })
      }
      else if (this.data.img_arr.length == 7) {   
        this.setData({
          gao:330
        })
      }
      else if (this.data.img_arr.length == 8) {     
        this.setData({
          gao: 330
        })
      }
      else if (this.data.img_arr.length == 9) {      
        this.setData({
          gao: 330
        })
      }
      else if (this.data.img_arr.length == 10) {
        this.setData({
          gao: 440
        })
      }
    } else {
      wx.showToast({
        title: '上传图片已经超限',
        icon: 'none',
        duration: 3000
      });
    }
  },

  previewImage(e) {
    var that = this;
    var cur = e.target.dataset.src; //获取本地一张图片链接
    ////console.log(cur)
    wx.previewImage({
      current: cur, //字符串，默认显示urls的第一张
      urls: [cur] // 数组，需要预览的图片链接列表
    })

  },
  guanbi:function(e){
    console.log(e)
    var id = e.currentTarget.dataset.id;
    var that = this;
    
    // app.globalData.file_12 = app.globalData.file_12.splice(id,1);
    for(var i = 0;i<app.globalData.file_12.length;i++){
      if(id==app.globalData.file_12[i]['id']){
        console.log(i)
        that.data.hktupian.splice(i,1);
        wx.showToast({
          title: '删除成功！',
          icon: 'none',
          duration: 1000
        })
      }
    }
    app.globalData.file_12 = that.data.hktupian
    console.log(app.globalData.file_12)
            that.setData({
              img_arr:app.globalData.file_12,
              hktupian:app.globalData.file_12
            })
    
    
        if(this.data.img_arr.length==1){
          this.setData({
            gao: 110 
          })
       } else if (this.data.img_arr.length == 2) {
         this.setData({
           gao: 110
         })
       }
       else if (this.data.img_arr.length == 3) {
         this.setData({
           gao: 220
         })
       }
       else if (this.data.img_arr.length == 4) {
         this.setData({
           gao: 220
         })
       }
       else if (this.data.img_arr.length == 5) {
         
         this.setData({
           gao: 220
         })
       }
       else if (this.data.img_arr.length == 6) {
        
         this.setData({
           gao: 220
         })
       }
       else if (this.data.img_arr.length == 7) {
         
         this.setData({
           gao:330
         })
       }
       else if (this.data.img_arr.length == 8) {
        
         this.setData({
           gao: 330
         })
       }
       else if (this.data.img_arr.length == 9) {
        
         this.setData({
           gao: 330
         })
       }
       else if (this.data.img_arr.length == 10) {
        
         this.setData({
           gao: 440
         })
       }
     
    
  },
  cl_x:function(e){
    this.setData({
      cl_x:e.target.dataset.id
    })    
  },
  gz_sq:function(e){
    console.log(e)
    this.data.gz_sq = e.target.dataset.id
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})